const menu = require('../model/Menu');
const ServerError = require('../../utils/ServerError');

module.exports = {
    loadMenuList: async (index = 0, limit = 10) => {
        let menu_count = await menu.loadMenuCount();

        if (menu_count < index * limit) {
            throw new ServerError(ServerError.CODE.ILLEGAL_PARAMS);
        }
        if (menu_count > 0) {
            let p_list = await menu.loadParentMenuList();
            let menu_list = await menu.loadMenuList(index, limit);

            return {total: menu_count, p_list: p_list, list: menu_list}
        }

        return {total: 0};

    },

    loadParentMenusList: async () => {
        return await menu.loadParentMenuList();
    },

    loadMenuByUser: async (user_id) => {
        return await menu.loadMenuByUser(user_id);
    },

    addMenu: async (param) => {
        return await menu.addMenu(param);
    },

    loadMenuById: async (menu_id) => {
        return await menu.loadMenuById(menu_id);
    },

    queryChildCount: async (menu_id) => {
        return await menu.queryChildMenuCount(menu_id);
    },

    queryMenuInUse: async (menu_id) => {
        let children = await menu.queryChildMenuCount(menu_id);
        let auth_count = await menu.queryMenuInUse(menu_id);
        return children || auth_count;
    },

    deleteMenu: async (menu_id) => {
        return await menu.deleteMenu(menu_id);
    },

    updateMenu: async (param) => {
        return await menu.updateMenu(param);
    },

    authMenu: async (admin_id, menu_ids) => {
        return await menu.authMenu(admin_id, menu_ids);
    },

    type : {
        TYPE_MAIN : 0,
        TYPE_PARENT: 1,
        TYPE_LEAF: 2
    }

};
